package main

import (
	"app/clients/hello/handler"
	"app/clientutils/middleware"
	pb "app/services/hello/proto"
	"github.com/aWildProgrammer/fconf"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
	"github.com/micro/go-log"
	"github.com/micro/go-micro/client"
	"github.com/micro/go-web"
)

func main() {
	c, err := fconf.NewFileConf("/Users/guijie/Documents/gowork/src/app/configs/service.ini")
	if err != nil {
		log.Fatal(err)
	}
	srvName := c.String("hello.service")
	api := c.String("hello.api")
	srvAddr := c.String("hello.host")
	port := c.String("hello.port")
	service := web.NewService(
		web.Name(api),
		web.Address(srvAddr+":"+port),
	)
	_ = service.Init()

	router := gin.Default()
	router.Use(middleware.Logger())
	router.Use(middleware.Cors())
	store, err := redis.NewStore(10, "tcp", "127.0.0.1:6379", "", []byte("secret"))
	if err != nil {
		log.Fatal(err)
	}
	router.Use(sessions.Sessions("mysession", store))
	srv := pb.NewSayService(srvName, client.DefaultClient)
	say := &handler.Say{Srv: srv}
	router.GET("/hello", say.Hello)
	service.Handle("/", router)
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
